//
//  WBNewFeatureView.swift
//  Weibo
//
//  Created by yinbo on 2018/3/15.
//  Copyright © 2018年 yinbo. All rights reserved.
//

import UIKit

class WBNewFeatureView: UIView {
    
    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var enterButton: UIButton!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    //进入微博
    @IBAction func enterStatus(_ sender: AnyObject) {
        removeFromSuperview()

    }
    class func  newfeatureView()->WBNewFeatureView{
        let nib = UINib(nibName:"WBNewFeatureView",bundle:nil)
        let v:WBNewFeatureView = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
        v.frame = UIScreen.main.bounds
        return v
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.orange
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

//        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        //如果使用自动布局设置的界面，从XIB加载默认600 * 600大小
        let count = 4
        let rect = UIScreen.main.bounds
        for i in 0..<count{
            let imageName = "new_feature_\(i + 1)"
            let iv = UIImageView(image:UIImage(named:imageName))
            
            //设置大小
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            scrollView.addSubview(iv)
        }
        
        //设置scrollView的属性
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        //影藏按钮
        enterButton.isHidden = true
        print(bounds)
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

extension WBNewFeatureView:UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x/scrollView.bounds.width)
        if page == scrollView.subviews.count{
            
            removeFromSuperview()
        }
//        if page == scrollView.subviews.count-1{
//            enterButton.isHidden = true
//        }
        enterButton.isHidden = (page != scrollView.subviews.count-1)
        print(page)
    }
    func scrollViewDidScroll(_
        scrollView: UIScrollView) {
        enterButton.isHidden = true
        let page = Int(scrollView.contentOffset.x/scrollView.bounds.width+0.5)
        pageControl.currentPage = page
        pageControl.isHidden = (page == scrollView.subviews.count)
        
        
    }
}
